﻿//Copyright (c) 2010 William Liu, Enoch Ye
//William Liu <william.lyz@gmail.com>
//Enoch Ye <moonlight.enoch@gmail.com>

//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at

//       http://www.apache.org/licenses/LICENSE-2.0

//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GBG_DBM.Support.DAL.Entities;
using GBG_DBM.Support.DAL;

namespace GBG_DBM.UI
{
    public partial class AddIngredient_UI : Form
    {
        private DBAccessory _DBAccessory = new DBAccessory();
        private Ingredient _Ingredient = new Ingredient();
        string[] Supplier_Code;
        string[] Supplier_Name;
        public AddIngredient_UI(Ingredient _Ingredient)
        {
            InitializeComponent();
            this._Ingredient = _Ingredient;
            DataBind();
        }
        public AddIngredient_UI(DataTable dt) 
        {
            InitializeComponent();
            this.RetrieveData(dt);
            cbbSupplierCode.Text = Convert.ToString(cbbSupplierCode.Items[0]);
            cbbSupplierName.Text=Convert.ToString(cbbSupplierName.Items[0]);
            
        }
        public AddIngredient_UI() 
        {
            InitializeComponent();
        }
        private void RetrieveData(DataTable dt) 
        {
            int count=dt.Rows.Count;
            for (int t = 0; t < count;t++)
            {
                cbbSupplierCode.Items.Add(Convert.ToString(dt.Rows[t].ItemArray[0]));
                cbbSupplierName.Items.Add(Convert.ToString(dt.Rows[t].ItemArray[1]));

            }
            
        }
        public AddIngredient_UI(string Code, string Name) 
        {
            InitializeComponent();
            cbbSupplierCode.Text = Code;
            cbbSupplierCode.Enabled = false;
            cbbSupplierName.Text = Name;
            cbbSupplierName.Enabled = false;
        }
        private void DataBind() 
        {
            cbbSupplierCode.Text = _Ingredient.SupplierCode;
            cbbSupplierName.Text = _Ingredient.SupplierName;
            cbbSupplierName.Enabled = false;
            cbbSupplierCode.Enabled = false;
            txtIngredientCode.Text = _Ingredient.IngredientCode;
            txtINCIName.Text = _Ingredient.INCIName;
            txtTradeName.Text = _Ingredient.TradeName;
            txtKeyProperty.Text = _Ingredient.KeyProperties;
            txtLocalPrice.Text = _Ingredient.LocalPrice.ToString(); ;
            txtIngredientCategory.Text = _Ingredient.Category;
        }
        private void Add_AddIngredient_UI_Click(object sender, EventArgs e)
        {
            if (_Ingredient.SupplierCode == null || _Ingredient.SupplierName == null)
            {
                if (cbbSupplierCode.Text.Length <= 0 || cbbSupplierName.Text.Length <= 0)
                {
                    MessageBox.Show("Please Select one");
                }
                else 
                {
                _Ingredient.IngredientCode = txtIngredientCode.Text;
                _Ingredient.Category = txtIngredientCategory.Text;
                _Ingredient.TradeName = txtTradeName.Text;
                _Ingredient.INCIName = txtINCIName.Text;
                _Ingredient.KeyProperties = txtKeyProperty.Text;
                _Ingredient.LocalPrice = Convert.ToSingle(txtLocalPrice.Text);
                _Ingredient.SupplierCode = cbbSupplierCode.SelectedItem.ToString();
                _Ingredient.SupplierName = cbbSupplierName.SelectedItem.ToString();
                _DBAccessory.addIngredient(_Ingredient);
                this.Close();
                }
            }
            else
            {
                _Ingredient.IngredientCode = txtIngredientCode.Text;
                _Ingredient.Category = txtIngredientCategory.Text;
                _Ingredient.TradeName = txtTradeName.Text;
                _Ingredient.INCIName = txtINCIName.Text;
                _Ingredient.KeyProperties = txtKeyProperty.Text;
                _Ingredient.LocalPrice = Convert.ToSingle(txtLocalPrice.Text);
                _Ingredient.SupplierCode = cbbSupplierCode.Text;
                _DBAccessory.addIngredient(_Ingredient);
                this.Close();
            }
        }

        private void Cancel_AddIngredient_UI_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
